﻿Imports System.IO
Imports System.Runtime.CompilerServices
Imports System.Threading

Public Class Form1
    Private fs1 As FileStream
    Private out
    Private list As New List(Of JiaMiTask)
    Public Class JiaMiTask
        Implements IDisposable, IConvertible
        Private _startd As Boolean
        Private _thread As Thread, _Name As String, _filename As String
        Public Sub New(thread As Thread, filename As String, Optional Name As String = "")
            _thread = thread
            _Name = Name
            _filename = filename
        End Sub
        <MethodImpl(MethodImplOptions.Synchronized)>
        Public Sub Start()
            _thread.Start(_filename)
            _startd = True
        End Sub
        Public ReadOnly Property Name As String
            Get
                If _Name = "" Then
                    Return _thread.Name
                End If
                Return _Name
            End Get
        End Property
        Public ReadOnly Property Thread As Thread
            Get
                Return _thread
            End Get
        End Property
#Region "IDisposable Support"
        Private disposedValue As Boolean ' 要检测冗余调用

        ' IDisposable
        Protected Overridable Sub Dispose(disposing As Boolean)
            If Not disposedValue Then
                If disposing Then
                    If _thread Is Nothing Then
                    ElseIf _thread.ThreadState = ThreadState.Running Then
                        _thread.Abort()
                    End If
                End If
                _thread = Nothing
                ' TODO: 释放未托管资源(未托管对象)并在以下内容中替代 Finalize()。
                ' TODO: 将大型字段设置为 null。
            End If
            disposedValue = True
        End Sub

        ' TODO: 仅当以上 Dispose(disposing As Boolean)拥有用于释放未托管资源的代码时才替代 Finalize()。
        'Protected Overrides Sub Finalize()
        '    ' 请勿更改此代码。将清理代码放入以上 Dispose(disposing As Boolean)中。
        '    Dispose(False)
        '    MyBase.Finalize()
        'End Sub

        ' Visual Basic 添加此代码以正确实现可释放模式。
        Public Sub Dispose() Implements IDisposable.Dispose
            ' 请勿更改此代码。将清理代码放入以上 Dispose(disposing As Boolean)中。
            Dispose(True)
            ' TODO: 如果在以上内容中替代了 Finalize()，则取消注释以下行。
            ' GC.SuppressFinalize(Me)
        End Sub

        Public Function GetTypeCode() As TypeCode Implements IConvertible.GetTypeCode
            Return _Name.GetTypeCode()
        End Function

        Public Function ToBoolean(provider As IFormatProvider) As Boolean Implements IConvertible.ToBoolean
            Return DirectCast(_Name, IConvertible).ToBoolean(provider)
        End Function

        Public Function ToChar(provider As IFormatProvider) As Char Implements IConvertible.ToChar
            Return DirectCast(_Name, IConvertible).ToChar(provider)
        End Function

        Public Function ToSByte(provider As IFormatProvider) As SByte Implements IConvertible.ToSByte
            Return DirectCast(_Name, IConvertible).ToSByte(provider)
        End Function

        Public Function ToByte(provider As IFormatProvider) As Byte Implements IConvertible.ToByte
            Return DirectCast(_Name, IConvertible).ToByte(provider)
        End Function

        Public Function ToInt16(provider As IFormatProvider) As Short Implements IConvertible.ToInt16
            Return DirectCast(_Name, IConvertible).ToInt16(provider)
        End Function

        Public Function ToUInt16(provider As IFormatProvider) As UShort Implements IConvertible.ToUInt16
            Return DirectCast(_Name, IConvertible).ToUInt16(provider)
        End Function

        Public Function ToInt32(provider As IFormatProvider) As Integer Implements IConvertible.ToInt32
            Return DirectCast(_Name, IConvertible).ToInt32(provider)
        End Function

        Public Function ToUInt32(provider As IFormatProvider) As UInteger Implements IConvertible.ToUInt32
            Return DirectCast(_Name, IConvertible).ToUInt32(provider)
        End Function

        Public Function ToInt64(provider As IFormatProvider) As Long Implements IConvertible.ToInt64
            Return DirectCast(_Name, IConvertible).ToInt64(provider)
        End Function

        Public Function ToUInt64(provider As IFormatProvider) As ULong Implements IConvertible.ToUInt64
            Return DirectCast(_Name, IConvertible).ToUInt64(provider)
        End Function

        Public Function ToSingle(provider As IFormatProvider) As Single Implements IConvertible.ToSingle
            Return DirectCast(_Name, IConvertible).ToSingle(provider)
        End Function

        Public Function ToDouble(provider As IFormatProvider) As Double Implements IConvertible.ToDouble
            Return DirectCast(_Name, IConvertible).ToDouble(provider)
        End Function

        Public Function ToDecimal(provider As IFormatProvider) As Decimal Implements IConvertible.ToDecimal
            Return DirectCast(_Name, IConvertible).ToDecimal(provider)
        End Function

        Public Function ToDateTime(provider As IFormatProvider) As Date Implements IConvertible.ToDateTime
            Return DirectCast(_Name, IConvertible).ToDateTime(provider)
        End Function

        Public Function ToString(provider As IFormatProvider) As String Implements IConvertible.ToString
            Return _Name.ToString(provider)
        End Function

        Public Function ToType(conversionType As Type, provider As IFormatProvider) As Object Implements IConvertible.ToType
            Return DirectCast(_Name, IConvertible).ToType(conversionType, provider)
        End Function
#End Region
    End Class
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim a As String
        a = System.Environment.CurrentDirectory
        OpenFileDialog1.InitialDirectory() = a
        Dim tmpop As Double = Me.Opacity
        Me.Opacity = 0
        TrackBar1.Value = tmpop * 100
        Me.Show()
        For i = 0 To CInt(tmpop * 100)
            Me.Opacity = i / 100
            Application.DoEvents()
            Threading.Thread.Sleep(50)
        Next
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Label1.Text = ""
        Label3.Text = ""
RetrieveVirtualItemEventArgs:
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            If IO.File.Exists(OpenFileDialog1.FileName) Then
                For Each wsw In OpenFileDialog1.FileNames
                    list.Add(New JiaMiTask(New Thread(AddressOf encordnc), wsw, wsw))
                Next
            Else
                MsgBox("无法打开的文件")
            End If
        Else
            GoTo RetrieveVirtualItemEventArgs
        End If
    End Sub
    Sub encordnc(filename As String)
        Dim fs1 As New FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)
        Dim buffer(fs1.Length) As Byte
        fs1.Read(buffer, 0, fs1.Length)
        Dim buffer2(fs1.Length) As Byte
        Dim p As Int32
        For Each w In buffer
            Dim ws As Byte = w Xor CByte(233)
            buffer2(p) = ws
            p += 1
        Next
        Dim nfs As New FileStream(IIf(TextBox2.Text = "", Environment.CurrentDirectory, TextBox2.Text) & "\" & Path.GetFileName(filename) & ".jiami", FileMode.Create)
        nfs.SetLength(fs1.Length)
        nfs.Write(buffer2, 0, buffer2.Length)
        buffer2 = Nothing
        buffer = Nothing
        fs1.Dispose()
        nfs.Dispose()
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
w:
        For iws = 0 To (list.Count - 1)
            If list.Item(iws).Thread.IsAlive Then
            Else
                list.RemoveAt(iws)
                GoTo w
            End If
        Next
        If wpoint = New Point(-1, -1) Then
        ElseIf actiond Then
            Me.Location = MousePosition - wpoint
        End If
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        For Each www In list
            www.Start()
        Next
        list.Clear()
    End Sub
    Private wpoint As Point = New Point(-1, -1)
    Private actiond As Boolean
    Private Sub Form1_Downm(sender As Object, e As EventArgs) Handles Me.MouseDown
        If Me.PointToClient(MousePosition).Y < 32 And Me.PointToClient(MousePosition).X > 1000 Then
            Environment.Exit(0)
        Else
            actiond = True
            wpoint = Me.PointToClient(MousePosition)
        End If
    End Sub

    Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll
        Me.Opacity = TrackBar1.Value / 100
    End Sub

    Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
        actiond = False
    End Sub
End Class
